$(document).ready(function(){
		if($("#poll-pick-date")){
			$("#poll-pick-date").datepicker({dateFormat : "dd.mm.yy"});
			$("#poll-pick-date").datepicker();
		}
});

function pollEditItem(item_id){
	$.post($("#pageURL").val()+"ajax/admin/configure/poll/edit-poll-item/"+ item_id +"/", function(data){
		if(data != "false"){
			$("#poll-edit-item-dialog").html(data);
			
			$("#poll-edit-item-dialog").dialog({autoOpen : true, width: 400, modal : true});
			if($("#poll-edit-item-dialog").dialog("isOpen") == false){
				$("#poll-edit-item-dialog").dialog("open");
			}	
		}
	});
}

function pollAddAnswer(add_translation, poll_id){
	
	var options = {autoOpen : true, width : 400, modal : true, buttons : {}};
	options["buttons"][add_translation] = function(){
		if($("#answer-label").val() != ""){
			$.post($("#pageURL").val()+"ajax/admin/configure/poll/update-poll-item/", {answer_label : $("#answer-label").val(), poll_id : poll_id}, function(data){
				if(data == "true"){
					location.href = $("#pageURL").val()+"admin/configure/poll/edit-poll/" + poll_id + "/";
				}
			});
		}
	};
	options["buttons"]["Close"] = function(){$("#poll-add-answer").dialog("close");};
	
	$("#poll-add-answer").dialog(options);
	
	if($("#poll-add-answer").dialog("isOpen") == false){
		$("#poll-add-answer").dialog("open");
	}	
}

function pollToggleEdit(menu){
	if(menu == "results"){
		$("#poll-edit-poll").hide();
		$("#poll-edit-items").hide();
		$("#poll-edit-results").show();
	}
	
	else if(menu == "config"){
		$("#poll-edit-results").hide();
		$("#poll-edit-items").hide();
		$("#poll-edit-poll").show();
	}
	else {
		$("#poll-edit-results").hide();
		$("#poll-edit-poll").hide();
		$("#poll-edit-items").show();
	}
}

function updatePoll(){
	var question = $("#poll-question").val();
	var active = $("#poll-active").val();
	var show_results = $("#poll-show-results").val();
	var members_only = $("#poll-members-only").val();
	
	var expires = $("#poll-pick-date").val();
	
	if(question == null || question == "" || active == null || active == "" || show_results == null || show_results == "" || members_only == null || members_only == "" || expires == null || expires == ""){
		// Dialog box
		$("#poll-add-error").dialog({
			autoOpen : true,
			width: 400,
			buttons: {
				"Close" : function(){
					$(this).dialog("close");
				}
			},
			modal : true
		});
		
		if($("#poll-add-error").dialog("isOpen") == false){
			$("#poll-add-error").dialog("open");
		}
	}
	else {
		$("#poll-form-add").submit();
	}
	return true;
}

function updatePollItem(item_id){
	answer = $("#poll-item-answer").val();
	votes = $("#poll-item-votes").val();
	pollid = $("#item-poll-id").val();
	
	$.post($("#pageURL").val() + "ajax/admin/configure/poll/update-poll-item/"+item_id+"/", {answer_label : answer, answer_votes : votes, poll_id : pollid},
			function(data){
				if(data == "true"){
					$("#poll-edit-item-dialog").dialog("close");
					location.href = $("#pageURL").val() + "admin/configure/poll/edit-poll/"+pollid+"/";
				}
	});
}

function pollVote(id){
	var url = $("#pageURL").val();
	
	$.post(url+"ajax/poll/vote/"+id+"/", function(data){
		if(data != "false"){
			showResults(id);
		}
	});
}
	
function showResults(poll_id){
	var url = $("#pageURL").val();
	$.post(url + "ajax/poll/results/" + poll_id, function(data){
		if(data != "false"){
			$("#poll-container").html(data);
		}
		else{
			var thanks_for_voting = $("#translation-poll-voting").val();
			$("#poll-container").html("<strong>" + thanks_for_voting + "</strong>");
		}
	});
}